package com.cheyun.netsalev3.view


import android.app.Dialog
import android.content.pm.PackageManager
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.MyApplication
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.databinding.ActivityWelComeBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.viewmodel.WelComeActivityViewModel
import com.cheyun.netsalev3.tencenttim.thirdpush.ThirdRegIdMgr
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyunkeji.er.utils.DensityUtil


class WelComeActivity : BaseActivity<ActivityWelComeBinding>() {
    private val TAG = WelComeActivity::class.java!!.getSimpleName()

    override fun initContentView(): Int {
        return R.layout.activity_wel_come
    }

    override fun initViewModel(): ViewModel? {
//        val formatter = SimpleDateFormat("yyyyMMddHHmmssSSS")
//        val time = formatter.format(Date())
//        Log.i("WelComeActivity","time initViewModel:"+time)
        var factory= InjectorUtils.getWelComeActivityViewModelFactory()
        return ViewModelProviders.of(this,factory).get(WelComeActivityViewModel::class.java)
    }

    override fun initView() {
        MyApplication.getInstance().registerEventBus()
        binding.viewModel?.activity = this

        ThirdRegIdMgr.prepareThirdPushToken(this)

//        val formatter = SimpleDateFormat("yyyyMMddHHmmssSSS")
//        val time = formatter.format(Date())
//        Log.i("WelComeActivity","time initView:"+time)
    }

    override fun initData() {
        binding?.viewModel?.apply {
            checkVersion(applicationContext,this@WelComeActivity)
        }

        binding.viewModel?.showYsWindow?.observe(this, Observer { it ->
            it?.apply {
                if(this){
                    showYs()
                }
            }
        })
        binding.viewModel?.showRefuseYsWindow?.observe(this, Observer { it ->
            it?.apply {
                if(this){
                    showRefuseYs()
                }
            }
        })
//        binding.viewModel?.versionUpdateData?.observe(this, Observer { data ->
//            showForce(data)
//        })
    }

    val REQUEST_EXTERNAL_STORAGE = 1

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        when (requestCode) {
            REQUEST_EXTERNAL_STORAGE->
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] ==PackageManager.PERMISSION_GRANTED) {
            } else {
                // Permission Denied 权限被拒绝
                binding.viewModel?.hasPermissions = false
            }
        }
        binding?.viewModel?.apply {
            checkVersion(applicationContext,this@WelComeActivity)
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    fun showYs(){
        val bottomDialog = Dialog(this, R.style.BottomDialog)
        val contentView = LayoutInflater.from(this).inflate(R.layout.dialog_agree_fuwu_ys, null)
        bottomDialog.setContentView(contentView)
        val params = contentView.layoutParams as ViewGroup.MarginLayoutParams
        params.width = this.getResources().getDisplayMetrics().widthPixels - DensityUtil.dip2px(this, 32f)
        params.bottomMargin = DensityUtil.dip2px(this, 8f)
        contentView.layoutParams = params
        bottomDialog.window!!.setGravity(Gravity.CENTER_VERTICAL)
//        bottomDialog.window!!.setWindowAnimations(R.style.BottomDialog_Animation)
        bottomDialog.show()

        var txtRefuseYs = contentView.findViewById<TextView>(R.id.txt_refuse_ys)
        var txtAgreeYs = contentView.findViewById<TextView>(R.id.txt_argee_ys)
        var txtYszc = contentView.findViewById<TextView>(R.id.txt_yszc)

        txtAgreeYs?.setOnClickListener {
            bottomDialog.dismiss()
            binding.viewModel?.confirmAgreeYsCallback()
        }

        txtRefuseYs?.setOnClickListener {
            bottomDialog.dismiss()
            binding.viewModel?.refuseYs()
        }

        txtYszc?.setOnClickListener {
            showYsWin()
        }
    }

    fun showRefuseYs(){
        val bottomDialog = Dialog(this, R.style.BottomDialog)
        val contentView = LayoutInflater.from(this).inflate(R.layout.dialog_refuse_fuwu_ys, null)
        bottomDialog.setContentView(contentView)
        val params = contentView.layoutParams as ViewGroup.MarginLayoutParams
        params.width = this.getResources().getDisplayMetrics().widthPixels - DensityUtil.dip2px(this, 32f)
        params.bottomMargin = DensityUtil.dip2px(this, 8f)
        contentView.layoutParams = params
        bottomDialog.window!!.setGravity(Gravity.CENTER_VERTICAL)
//        bottomDialog.window!!.setWindowAnimations(R.style.BottomDialog_Animation)
        bottomDialog.show()

        var txtRefuseYs = contentView.findViewById<TextView>(R.id.txt_refuse_ys)
        var txtAgreeYs = contentView.findViewById<TextView>(R.id.txt_argee_ys)
        var txtYszc = contentView.findViewById<TextView>(R.id.txt_yszc)

        txtAgreeYs?.setOnClickListener {
            bottomDialog.dismiss()
            binding.viewModel?.confirmAgreeYsCallback()
        }

        txtRefuseYs?.setOnClickListener {
            bottomDialog.dismiss()
            finish()
        }
        txtYszc?.setOnClickListener {
            showYsWin()
        }
    }

    fun showYsWin(){
        val bottomDialog = Dialog(this, R.style.BottomDialog)
        val contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_circle, null)
        bottomDialog.setContentView(contentView)
        val params = contentView.layoutParams as ViewGroup.MarginLayoutParams
        params.width = this.getResources().getDisplayMetrics().widthPixels - DensityUtil.dip2px(this, 42f)
        params.topMargin = DensityUtil.dip2px(this, 20f)
        params.bottomMargin = DensityUtil.dip2px(this, 20f)
        contentView.layoutParams = params
        bottomDialog.window!!.setGravity(Gravity.BOTTOM)
//        bottomDialog.window!!.setWindowAnimations(R.style.BottomDialog_Animation)
        bottomDialog.show()

        var llWin = contentView.findViewById<LinearLayout>(R.id.ll_win)
        llWin.setOnClickListener { bottomDialog.dismiss() }
    }



}
